package by.parfen.game.test;

import java.net.URI;
import java.net.URISyntaxException;

import by.parfen.game.atrash.client.GameWebsocketClient;
import by.parfen.game.service.GameMessageService;
import by.parfen.game.service.impl.GameMessageServiceImpl;

public class PlayerPage {

	private static final String WS_ROOT_URL = "ws://localhost:8180/duelgame/websocket-game/root";

	GameMessageService gameMessageService;

	GameWebsocketClient clientEndpoint;

	public PlayerPage() {
		try {
			clientEndpoint = new GameWebsocketClient(new URI(WS_ROOT_URL));
			clientEndpoint.addMessageHandler(new MessageHandler());

			gameMessageService = new GameMessageServiceImpl();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void login(String name) {
		// clientEndpoint.sendMessage(gameMessageService.convertMessage2String(gameMessageService.createLoginMessage(name)));
		clientEndpoint.sendMessage(gameMessageService.encode(gameMessageService.createLoginMessage(name)).toString());
	}

	public void invite(String inviter, String player) {
		// clientEndpoint
		// .sendMessage(gameMessageService.convertMessage2String(gameMessageService.createInviteMessage(inviter,
		// player)));
		clientEndpoint
				.sendMessage(gameMessageService.encode(gameMessageService.createInviteMessage(inviter, player)).toString());
	}

	// handle onMessage
	private class MessageHandler implements GameWebsocketClient.MessageHandler {
		public void handleMessage(String message) {
			System.out.println("handled Message: " + message);
		}
	}

}
